<?php

namespace App\Models;

class Dining extends Base
{
    //分页获取列表数据
    public function getPageList($options=[],$where=[],$whereDates){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = $this->where($where)->orderBy('created_at','desc');
            $result = $this->paging($model,$page,$limit);
            if(!$result['code']){
                foreach ($result['data']['list'] as &$item){
                    $order = (new DiningReserve())->leftJoin('users','users.id','=','dining_reserves.uid')->whereDate('sitday','>=',$whereDates[0])
                            ->whereDate('sitday','<',$whereDates[1])->where([ ['tid','=',$item['id']],['timetype','=',$options['timetype']] ])
                            ->select('dining_reserves.ordersn','dining_reserves.timetype','dining_reserves.status','dining_reserves.pernum','dining_reserves.sitday','users.contact','users.username_zn')
                            ->whereIn('status',[1,2,3])->first();
                    $item['dinorder'] = $order;
                    if($order){
                        if($order->status==2){ //待进餐
                            $item['status'] = 1; // 已预订，未到场
                        }else if($order->status == 3){
                            $item['status'] = 2; // 已到场，未结账
                        }else if($order->status ==1){
                            $item['status']= 3; // 客人预定中
                        }else{
                            $item['status'] = 0; // 空桌
                        }
                    }else{
                        $item['status'] = 0; // 空桌
                    }
                }
            }
            return $result;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //新增单个餐桌
    public function oneInsert($data=array()){
        try {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
            $id = $this->insertGetId($data);
            if($id){
                return [
                    'code'=>0,
                    'data'=>$id
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //编辑单个餐桌
    public function doUpdate($data){
        try {
            $now = date('Y-m-d H:i:s');
            $data['updated_at'] = $now;
            $res = $this->where('id',$data['id'])->update($data);
            if($res){
                return [
                    'code'=>0,
                    'data'=>$data['id']
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'更新失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }
}
